<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/2
 * Time: 13:49
 */

namespace app\api\service;

use app\api\model\StaffInfo as StaffInfoModel;
use app\api\model\Images;
use app\facade\Basic;
class Staff
{

    protected $model;

    public function __construct()
    {
        $this->model = new StaffInfoModel();

    }

    /**
     * @describe 获取销售人员列表
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function getList( $page, $limit,$lang): array
    {
        if (0 >= $page || 100 < $limit) {
            return Basic::data( 100002, 'common.parameterError');
        }
        $where = ['is_del' => 0,'if_show'=>1];
        $total = $this->model->where($where)->count();
        if (!$total) {
            return Basic::data(0, 'common.success', ['list' => [], 'total' => 0]);
        }
        $list = $this->model->where($where)
            ->field('worknumber,fullname,avator,job,describe,support_language,ins,linkedin,facebook,zaloid,email,phone,telegramid,twitterid,tiktokid,lineid,wechat,whatsapp,website')
            ->page($page, $limit)
            ->select()->toArray();
        $avatorId = array_unique(array_column($list,'avator'));
        #获取图片url
        $oImages = new Images();
        $avatorUrls = $oImages->getImagesByIds($avatorId);
        $avatorUrls = array_column($avatorUrls, null, 'id');
        #替换url
        #翻译支持语言
        $common = new Common();
        foreach ($list as $k=>$v){
            $list[$k]['support_language_zh'] = [];
            $list[$k]['describe'] = htmlspecialchars_decode($v['describe'], ENT_QUOTES);
            $list[$k]['avator'] = $avatorUrls[$v['avator']]['url']??'';
            $list[$k]['name'] = $avatorUrls[$v['avator']]['name']??'';
            $list[$k]['avatorId'] = $avatorUrls[$v['avator']]['id']??'';
            #处理翻译
            if( !empty($v['support_language'])){
                $trans = [];
                $support_language = explode(',',$v['support_language']);
                foreach ($support_language as $lan ){
                    $trans[] = $common->singleTranslate($lan,$lang);
                }
                if($trans){
                    $list[$k]['support_language_zh'] = $trans;
                }

            }
        }
        return Basic::data(0, 'common.success', ['list' => $list, 'total' => $total]);
    }


    /**
     * @describe 登录验证
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function auth($params): array
    {
        #参数校验
        if (isset($params['type']) && !in_array($params['type'],['email','phone','telegramid','twitterid','tiktokid','lineid','wechat','whatsapp','website'])) {
            return Basic::data( 100002, 'common.parameterError');
        }
        $where = [
                    'is_del' => 0,
                    $params['type'] =>$params['content']
            ];

        $exists= $this->model->where($where)->count();
        $auth = $exists>0?true:false;

        return Basic::data(0, 'common.success',$auth);
    }

}